package com.cwj.mansysback.dao.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtil {

    public static Connection getConnection() throws SQLException, IOException, ClassNotFoundException {
        try(InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");){

            Properties properties = new Properties();

            properties.load(in);

            String user=properties.getProperty("user");
            String password=properties.getProperty("password");
            String diverClass=properties.getProperty("diverclass");
            String url=properties.getProperty("url");

            Class.forName(diverClass);

            return DriverManager.getConnection(url,user,password);
        } catch (IOException | SQLException | ClassNotFoundException e) {
            throw e;
        }

    }

    public static void close(PreparedStatement ps,Connection con,boolean isAutoCommit) throws SQLException {


        if(ps != null){
            ps.close();
        }

        if(con != null){
            con.setAutoCommit(isAutoCommit);
            con.close();
        }
    }

}
